package org.skyfree.ghyll.tcard.progress;

import java.util.ArrayList;

import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.PartInitException;
import org.skyfree.ghyll.tcard.core.IResourceChangeEvent;
import org.skyfree.ghyll.tcard.core.IResourceChangedListener;
import org.skyfree.ghyll.tcard.core.ITProject;
import org.skyfree.ghyll.tcard.core.ITWorkpiece;
import org.skyfree.ghyll.tcard.core.ITWorkspace;
import org.skyfree.ghyll.tcard.core.TCardCore;
import org.skyfree.ghyll.ui.part.SelectionEnableViewPart;

public class ProgressExplorer extends SelectionEnableViewPart implements IDoubleClickListener, ISelectionChangedListener, IResourceChangedListener {

	class TreeLabelProvider extends LabelProvider 
	{
		public String getText(Object element) 
		{
			if(element instanceof ITProject)
			{
				return ((ITProject)element).getName();
			}
			else if(element instanceof ITWorkpiece)
			{
				return ((ITWorkpiece)element).getName();
			}
			return super.getText(element);
		}
		public Image getImage(Object element) 
		{
			if(element instanceof ITProject)
			{
				return ICON_Project;
			}
			else if(element instanceof ITWorkpiece)
			{
				return ICON_WP;
			}
			return null;
		}
	}
	static Image ICON_Project = Activator.getImageDescriptor("icons/project.png").createImage();
	static Image ICON_WP = Activator.getImageDescriptor("icons/workpiece.png").createImage();
	
	class TreeContentProvider implements IStructuredContentProvider, ITreeContentProvider 
	{
		ITWorkspace WSHandle = TCardCore.getDefault().getWorkSpace();
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
		public void dispose() {
		}
		public Object[] getElements(Object inputElement) 
		{
			return WSHandle.getProjects();
		}
		public Object[] getChildren(Object parentElement)
		{
			if(parentElement instanceof ITProject)
			{
				ITProject project = (ITProject)parentElement;
				ArrayList<ITWorkpiece> inProList = new ArrayList<ITWorkpiece>(); 
				ITWorkpiece[] allwp = project.getAllWorkpiece();
				for(int i=0;i<allwp.length;i++)
				{
					ITWorkpiece wp = allwp[i];
					if(wp.isInProgress())inProList.add(wp);
				}
				return inProList.toArray();
			}
			else 
			{
				return new Object[0];
			}
		}
		public Object getParent(Object element) 
		{
			
			if(element instanceof ITWorkpiece)
			{
				ITWorkpiece wp = (ITWorkpiece)element;
				return wp.getAdapter(ITProject.class);
			}
			else 
			{
				return null;
			}
		}
		public boolean hasChildren(Object element) {
			return getChildren(element).length > 0;
		}
	}
	private TreeViewer treeViewer;
	public static final String ID = "org.skyfree.ghyll.tcard.progress.ProgressExplorer"; //$NON-NLS-1$

	/**
	 * Create contents of the view part
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new FillLayout());

		this.treeViewer = new TreeViewer(container, SWT.BORDER);
		treeViewer.setLabelProvider(new TreeLabelProvider());
		treeViewer.setContentProvider(new TreeContentProvider());
		treeViewer.setInput(new Object());
		treeViewer.addDoubleClickListener(this);
		treeViewer.addSelectionChangedListener(this);
		
		initializeToolBar();
		
		TCardCore.getDefault().addResourceChangedListener(this);
		
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

	@Override
	public void doubleClick(DoubleClickEvent event) 
	{
		TreeSelection selection = (TreeSelection)this.treeViewer.getSelection();
		Object select = selection.getFirstElement();
		if(select instanceof ITWorkpiece)
		{
			openWorkpiece((ITWorkpiece)select);
		}
	}

	public TreeViewer getTreeViewer() {
		return treeViewer;
	}

	@Override
	public void selectionChanged(SelectionChangedEvent event)
	{
		this.fireSelectionEvent(event);
		
		//this.treeViewer.setSelection(new StructuredSelection(wp), true);
		//this.treeViewer.setExpandedElements(new Object[]{wp});
		/*TreeItem se = this.treeViewer.getTree().getSelection()[0];
		se.getItems()[0].*/
	}
	private void initializeToolBar() 
	{
		//IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
	}

	public void focusOn(ITWorkpiece wp)
	{
		this.treeViewer.getTree().forceFocus();
		this.treeViewer.expandToLevel(wp, 0);
		this.treeViewer.setSelection(new StructuredSelection(wp), true);
	}
	
	public WpProgressEditor openWorkpiece(ITWorkpiece wp)
	{
		InProgressWorkpieceInput input = new InProgressWorkpieceInput(wp.adapter4Inprogress());
		try
		{
			boolean opened = false;
			IEditorReference[] allEditor = this.getViewSite().getWorkbenchWindow().getActivePage().getEditorReferences();
			for(int i=0;i<allEditor.length;i++)
			{
				IEditorInput theinput = allEditor[i].getEditorInput();
				if(theinput instanceof InProgressWorkpieceInput)
				{
					InProgressWorkpieceInput ipw = (InProgressWorkpieceInput)theinput;
					if(ipw.getWp().getPath().equals(input.getWp().getPath()))
					{
						opened = true;
						WpProgressEditor editor = (WpProgressEditor) allEditor[i].getEditor(true);
						this.getViewSite().getWorkbenchWindow().getActivePage().activate(editor);
						return editor;
					}
				}
			}
			if(!opened)
			{
				return (WpProgressEditor) this.getViewSite().getWorkbenchWindow().getActivePage().openEditor(input, WpProgressEditor.ID);
			}
		}catch (PartInitException ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		}
		return null;
	}

	@Override
	public void onResourceChanged(IResourceChangeEvent event) 
	{
		if(event.getType().equals(IResourceChangeEvent.Type_Progress))
		{
			this.treeViewer.refresh();
		}
		
	}
}
